﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using V5_Model;

namespace V5_DAL
{
    public class DALProductComment
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_ProductComment");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductComment");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductComment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_ProductComment(");
            strSql.Append("ID,CommentContent,CommentName,CommentEmail,CommentIP,ProductName,ProductID,AddDateTime,ReplyComment,ReplyName,Pmost,MemberID,Pmost1,Pmost2,Pmost3,Passed)");
            strSql.Append(" values (");
            strSql.Append("@ID,@CommentContent,@CommentName,@CommentEmail,@CommentIP,@ProductName,@ProductID,@AddDateTime,@ReplyComment,@ReplyName,@Pmost,@MemberID,@Pmost1,@Pmost2,@Pmost3,@Passed)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@CommentContent", SqlDbType.NVarChar,255),
					new SqlParameter("@CommentName", SqlDbType.NVarChar,50),
					new SqlParameter("@CommentEmail", SqlDbType.NVarChar,255),
					new SqlParameter("@CommentIP", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@ReplyComment", SqlDbType.NVarChar,255),
					new SqlParameter("@ReplyName", SqlDbType.NVarChar,50),
					new SqlParameter("@Pmost", SqlDbType.Int,4),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@Pmost1", SqlDbType.Int,4),
					new SqlParameter("@Pmost2", SqlDbType.Int,4),
					new SqlParameter("@Pmost3", SqlDbType.Int,4),
					new SqlParameter("@Passed", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CommentContent;
            parameters[2].Value = model.CommentName;
            parameters[3].Value = model.CommentEmail;
            parameters[4].Value = model.CommentIP;
            parameters[5].Value = model.ProductName;
            parameters[6].Value = model.ProductID;
            parameters[7].Value = model.AddDateTime;
            parameters[8].Value = model.ReplyComment;
            parameters[9].Value = model.ReplyName;
            parameters[10].Value = model.Pmost;
            parameters[11].Value = model.MemberID;
            parameters[12].Value = model.Pmost1;
            parameters[13].Value = model.Pmost2;
            parameters[14].Value = model.Pmost3;
            parameters[15].Value = model.Passed;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelProductComment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductComment set ");
            strSql.Append("CommentContent=@CommentContent,");
            strSql.Append("CommentName=@CommentName,");
            strSql.Append("CommentEmail=@CommentEmail,");
            strSql.Append("CommentIP=@CommentIP,");
            strSql.Append("ProductName=@ProductName,");
            strSql.Append("ProductID=@ProductID,");
            strSql.Append("AddDateTime=@AddDateTime,");
            strSql.Append("ReplyComment=@ReplyComment,");
            strSql.Append("ReplyName=@ReplyName,");
            strSql.Append("Pmost=@Pmost,");
            strSql.Append("MemberID=@MemberID,");
            strSql.Append("Pmost1=@Pmost1,");
            strSql.Append("Pmost2=@Pmost2,");
            strSql.Append("Pmost3=@Pmost3,");
            strSql.Append("Passed=@Passed");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@CommentContent", SqlDbType.NVarChar,255),
					new SqlParameter("@CommentName", SqlDbType.NVarChar,50),
					new SqlParameter("@CommentEmail", SqlDbType.NVarChar,255),
					new SqlParameter("@CommentIP", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductID", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@ReplyComment", SqlDbType.NVarChar,255),
					new SqlParameter("@ReplyName", SqlDbType.NVarChar,50),
					new SqlParameter("@Pmost", SqlDbType.Int,4),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@Pmost1", SqlDbType.Int,4),
					new SqlParameter("@Pmost2", SqlDbType.Int,4),
					new SqlParameter("@Pmost3", SqlDbType.Int,4),
					new SqlParameter("@Passed", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CommentContent;
            parameters[2].Value = model.CommentName;
            parameters[3].Value = model.CommentEmail;
            parameters[4].Value = model.CommentIP;
            parameters[5].Value = model.ProductName;
            parameters[6].Value = model.ProductID;
            parameters[7].Value = model.AddDateTime;
            parameters[8].Value = model.ReplyComment;
            parameters[9].Value = model.ReplyName;
            parameters[10].Value = model.Pmost;
            parameters[11].Value = model.MemberID;
            parameters[12].Value = model.Pmost1;
            parameters[13].Value = model.Pmost2;
            parameters[14].Value = model.Pmost3;
            parameters[15].Value = model.Passed;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductComment ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductComment GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,CommentContent,CommentName,CommentEmail,CommentIP,ProductName,ProductID,AddDateTime,ReplyComment,ReplyName,Pmost,MemberID,Pmost1,Pmost2,Pmost3,Passed from K_ProductComment ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelProductComment model = new ModelProductComment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.CommentContent = ds.Tables[0].Rows[0]["CommentContent"].ToString();
                model.CommentName = ds.Tables[0].Rows[0]["CommentName"].ToString();
                model.CommentEmail = ds.Tables[0].Rows[0]["CommentEmail"].ToString();
                model.CommentIP = ds.Tables[0].Rows[0]["CommentIP"].ToString();
                model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString();
                if (ds.Tables[0].Rows[0]["ProductID"].ToString() != "")
                {
                    model.ProductID = int.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "")
                {
                    model.AddDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                model.ReplyComment = ds.Tables[0].Rows[0]["ReplyComment"].ToString();
                model.ReplyName = ds.Tables[0].Rows[0]["ReplyName"].ToString();
                if (ds.Tables[0].Rows[0]["Pmost"].ToString() != "")
                {
                    model.Pmost = int.Parse(ds.Tables[0].Rows[0]["Pmost"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                {
                    model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost1"].ToString() != "")
                {
                    model.Pmost1 = int.Parse(ds.Tables[0].Rows[0]["Pmost1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost2"].ToString() != "")
                {
                    model.Pmost2 = int.Parse(ds.Tables[0].Rows[0]["Pmost2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pmost3"].ToString() != "")
                {
                    model.Pmost3 = int.Parse(ds.Tables[0].Rows[0]["Pmost3"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Passed"].ToString() != "")
                {
                    model.Passed = int.Parse(ds.Tables[0].Rows[0]["Passed"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,CommentContent,CommentName,CommentEmail,CommentIP,ProductName,ProductID,AddDateTime,ReplyComment,ReplyName,Pmost,MemberID,Pmost1,Pmost2,Pmost3,Passed ");
            strSql.Append(" FROM K_ProductComment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,CommentContent,CommentName,CommentEmail,CommentIP,ProductName,ProductID,AddDateTime,ReplyComment,ReplyName,Pmost,MemberID,Pmost1,Pmost2,Pmost3,Passed ");
            strSql.Append(" FROM K_ProductComment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_ProductComment";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  成员方法
    }
}
